package com.ostrich.cloud.ostrichGoodsService.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ostrich.cloud.common.exceptions.OstrichException;
import com.ostrich.cloud.common.result.PageResult;
import com.ostrich.cloud.ostrichGoodsSercice.model.Dto.TypeDto;
import com.ostrich.cloud.ostrichGoodsService.common.exception.OstrichGoodsException;
import com.ostrich.cloud.ostrichGoodsService.common.exception.OstrichGoodsExceptionCode;
import com.ostrich.cloud.ostrichGoodsService.infra.mapper.GoodsMapper;
import com.ostrich.cloud.ostrichGoodsService.infra.model.Goods;
import com.ostrich.cloud.ostrichGoodsService.infra.model.Type;
import com.ostrich.cloud.ostrichGoodsService.infra.mapper.TypeMapper;
import com.ostrich.cloud.ostrichGoodsService.service.TypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ostrich.cloud.ostrichGoodsService.service.param.CreateTypeParam;
import com.ostrich.cloud.ostrichGoodsService.service.param.GetTypeParam;
import com.ostrich.cloud.ostrichGoodsService.service.param.UpdateTypeParam;
import lombok.extern.slf4j.Slf4j;
import org.omg.CORBA.INTERNAL;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 类别表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2022-10-26
 */
@Service
@Slf4j
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements TypeService {

    @Resource
    private TypeMapper typeMapper;

    @Resource
    private GoodsMapper goodsMapper;
    @Override
    @Transactional
    public void createType(CreateTypeParam createTypeParam) {
        Type type = BeanUtil.copyProperties(createTypeParam, Type.class);
        if(type.getTypeParentId()==null){
            type.setTypeParentId(0);
        }
        try {
            int insert = typeMapper.insert(type);
        }catch (OstrichGoodsException e){
            throw new OstrichGoodsException(OstrichGoodsExceptionCode.TYPE_DEL_DB_EXCEPTION);
        }


    }

    @Override
    public PageResult<TypeDto> getAllType(GetTypeParam param) {
        if(param.getTypeParentId()==null){
            param.setTypeParentId(0);
        }
        QueryWrapper<Type> wrapper=new QueryWrapper<>();
        wrapper.like(param.getTypeName()!=null,"type_name",param.getTypeName())
                .eq("type_parent_id",param.getTypeParentId());

        PageResult<TypeDto> typeDtoPageResult = null;
        IPage<Type> typeIPage = typeMapper.selectPage(new Page<Type>(param.getCurrent(), param.getSize()), wrapper);
        if(typeIPage!=null&&typeIPage.getRecords().size()!=0){
            typeDtoPageResult =new PageResult<>();
            List<TypeDto> typeDto = BeanUtil.copyToList(typeIPage.getRecords(), TypeDto.class);
            typeDtoPageResult.setElements(typeDto);
            typeDtoPageResult.setPage(typeIPage.getCurrent());
            typeDtoPageResult.setPageSize(typeIPage.getSize());
            typeDtoPageResult.setTotal(typeIPage.getTotal());
            typeDtoPageResult.setTotalPage(typeIPage.getPages());
        }

        return typeDtoPageResult;
    }

    @Override
    @Transactional
    public Boolean delType(Integer typeId) {
        Type type = typeMapper.selectById(typeId);
        if(type==null){
            throw new OstrichGoodsException(OstrichGoodsExceptionCode.TYPE_GET_EXCEPTION);
        }
        QueryWrapper<Type> wrapper = new QueryWrapper<>();
        wrapper.eq("type_parent_id",type.getTypeId());
        List<Type> types = typeMapper.selectList(wrapper);
        if(types!=null&&types.size()!=0){
            throw new OstrichGoodsException(OstrichGoodsExceptionCode.TYPE_DEL_SON_EXCEPTION);
        }
        QueryWrapper<Goods> gqw = new QueryWrapper<>();
        gqw.eq("type_id",typeId);
        List<Goods> goods = goodsMapper.selectList(gqw);
        if(goods!=null&&goods.size()!=0){
            throw new OstrichGoodsException(OstrichGoodsExceptionCode.TYPE_DEL_GOODS_EXCEPTION);
        }
        try {
            int i = typeMapper.deleteById(typeId);
        }catch (OstrichGoodsException e){
            throw new OstrichGoodsException(OstrichGoodsExceptionCode.TYPE_DEL_DB_EXCEPTION);
        }

        return true;
    }

    @Override
    public Boolean updateType(UpdateTypeParam updateTypeParam) {
        Type type = BeanUtil.copyProperties(updateTypeParam, Type.class);
        int i = typeMapper.updateById(type);
        if(i<1){
            throw new OstrichGoodsException(OstrichGoodsExceptionCode.TYPE_DEL_DB_EXCEPTION);
        }

        return true;
    }


}
